home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / sysdeps / generic / memmove.c < prev    next >
C/C++ Source or Header  |  1994-04-26  |  3KB  |  100 lines

  1. /* memmove -- copy memory to memory until the specified number of bytes
  2.    has been copied.  Overlap is handled correctly.
  3.    Copyright (C) 1991 Free Software Foundation, Inc.
  4.    Contributed by Torbjorn Granlund (tege@sics.se).
  5.  
  6. The GNU C Library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Library General Public License as
  8. published by the Free Software Foundation; either version 2 of the
  9. License, or (at your option) any later version.
  10.  
  11. The GNU C Library is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14. Library General Public License for more details.
  15.  
  16. You should have received a copy of the GNU Library General Public
  17. License along with the GNU C Library; see the file COPYING.LIB.  If
  18. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  19. Cambridge, MA 02139, USA.  */
  20.  
  21. #include <ansidecl.h>
  22. #include <string.h>
  23. #include <memcopy.h>
  24.  
  25. /* All this is so that bcopy.c can #include
  26.    this file after defining some things.  */
  27. #ifndef    a1
  28. #define    a1    dest    /* First arg is DEST.  */
  29. #define    a1const
  30. #define    a2    src    /* Second arg is SRC.  */
  31. #define    a2const    CONST
  32. #endif
  33. #if    !defined(RETURN) || !defined(rettype)
  34. #define    RETURN(s)    return (s)    /* Return DEST.  */
  35. #define    rettype        PTR
  36. #endif
  37.  
  38. rettype
  39. DEFUN(memmove, (a1, a2, len),
  40.       a1const PTR a1 AND a2const PTR a2 AND size_t len)
  41. {
  42.   unsigned long int dstp = (long int) dest;
  43.   unsigned long int srcp = (long int) src;
  44.  
  45.   /* This test makes the forward copying code be used whenever possible.
  46.      Reduces the working set.  */
  47.   if (dstp - srcp >= len)    /* *Unsigned* compare!  */
  48.     {
  49.       /* Copy from the beginning to the end.  */
  50.  
  51.       /* If there not too few bytes to copy, use word copy.  */
  52.       if (len >= OP_T_THRES)
  53.     {
  54.       /* Copy just a few bytes to make DSTP aligned.  */
  55.       len -= (-dstp) % OPSIZ;
  56.       BYTE_COPY_FWD (dstp, srcp, (-dstp) % OPSIZ);
  57.  
  58.       /* Copy from SRCP to DSTP taking advantage of the known
  59.          alignment of DSTP.  Number of bytes remaining is put
  60.          in the third argumnet, i.e. in LEN.  This number may
  61.          vary from machine to machine.  */
  62.  
  63.       WORD_COPY_FWD (dstp, srcp, len, len);
  64.  
  65.       /* Fall out and copy the tail.  */
  66.     }
  67.  
  68.       /* There are just a few bytes to copy.  Use byte memory operations.  */
  69.       BYTE_COPY_FWD (dstp, srcp, len);
  70.     }
  71.   else
  72.     {
  73.       /* Copy from the end to the beginning.  */
  74.       srcp += len;
  75.       dstp += len;
  76.  
  77.       /* If there not too few bytes to copy, use word copy.  */
  78.       if (len >= OP_T_THRES)
  79.     {
  80.       /* Copy just a few bytes to make DSTP aligned.  */
  81.       len -= dstp % OPSIZ;
  82.       BYTE_COPY_BWD (dstp, srcp, dstp % OPSIZ);
  83.  
  84.       /* Copy from SRCP to DSTP taking advantage of the known
  85.          alignment of DSTP.  Number of bytes remaining is put
  86.          in the third argumnet, i.e. in LEN.  This number may
  87.          vary from machine to machine.  */
  88.  
  89.       WORD_COPY_BWD (dstp, srcp, len, len);
  90.  
  91.       /* Fall out and copy the tail.  */
  92.     }
  93.  
  94.       /* There are just a few bytes to copy.  Use byte memory operations.  */
  95.       BYTE_COPY_BWD (dstp, srcp, len);
  96.     }
  97.  
  98.   RETURN(dest);
  99. }
  100.